﻿package org.sunxin.ch05.servlet;

import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;

import org.sunxin.ch05.util.OutputSessionInfo;

public class LoginServlet extends HttpServlet
{
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
               throws ServletException,IOException
    {
        resp.setContentType("text/html;charset=gb2312");
        
        HttpSession session=req.getSession();
        String user=(String)session.getAttribute("user");
        
        PrintWriter out=resp.getWriter();
        out.println("<html>");
        out.println("<meta http-equiv=\"Pragma\" content=\"no-cache\">");
        out.println("<head><title>登录页面</title></head>");
        out.println("<body>");
        
        OutputSessionInfo.printSessionInfo(out,session);
        
        out.println("<p>");
        out.println("<form method=post action="+resp.encodeURL("loginchk")+">");
    
        out.println("<table>");
        
        out.println("<tr>");
        out.println("<td>请输入用户名</td>");
        if(null==user)
        {
            out.println("<td><input type=text name=user></td>");
        }
        else
        {
            out.println("<td><input type=text name=user value="+user+"></td>");
        }
        out.println("</tr>");
        
        out.println("<tr>");
        out.println("<td>请输入密码</td>");
        out.println("<td><input type=password name=password></td>");
        out.println("</tr>");
        
        out.println("<tr>");
        out.println("<td><input type=reset value=重填></td>");
        out.println("<td><input type=submit value=登录></td>");
        out.println("</tr>");
        
        out.println("</table>");
    
        out.println("</form>");
        out.println("</body>");
        out.println("</html>");
        out.close();
    }
    
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
               throws ServletException,IOException
    {
        doGet(req,resp);
    }
}